﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; 
using Microsoft.Practices.Unity;
using Exhibition.Dao.ViewModel;
using Exhibition.Common.Helper;
using Exhibition.Common.Web;
using Exhibition.Service;
using Exhibition.Common.CacheManager;

namespace Exhibition.Service
{
   public class BaseAdminPage:System.Web.UI.Page
   {



       public string ImgUrl { get { return StringUtils.GetWebConfigValue("ImgUrl"); } }

       public SiteModel stModl { get; set; }
       public int reqID { get; set; }
       public AdminUserModel AdminUser { get; set; }
       public int catcheSiteID { get; set; } 
       public SiteModel CurrentSite { get; set; }
       
       protected override void OnInit(EventArgs e)
       {
 

           if (!string.IsNullOrEmpty(Request["ID"]))
           {
               try
               {

                   reqID = Convert.ToInt32(Request["ID"]);
               }
               catch (Exception)
               {
                   reqID = 0;
                   //  LogHelper.Error("获取的ID不为数字！" + Request["ID"]);
               }
           }

           if (MembershipService.GetUser() == null)
           {
               // Response.Redirect("~/Login.aspx");
               JsHelper.Redirect("../AdminLogin.aspx", "iframe");

               return;
           }
           AdminUser = MembershipService.GetUser(); 
           //需要根据用户来  如果管理员切换网站stModl必须有  否则其他用户登陆 就不存在这个 默认为系统当前  
           //缓存切换的网站ID 
           catcheSiteID = CommonCacheManager.GetCacheData<int>("SiteID" + AdminUser.Id);

           if (Request["SiteID"] != null)
           {
               //
               catcheSiteID = Convert.ToInt32(Request["SiteID"]);
               stModl = CenterService.siteService.GetOneById(catcheSiteID);
               CommonCacheManager.AddCacheData<int>("SiteID" + AdminUser.Id, Convert.ToInt32(Request["SiteID"]), 180);
               //将切换的网站放入缓存 这个只有管理员有权限
               CommonCacheManager.AddCacheData<SiteModel>("stModl" + AdminUser.Id, stModl, 180);
                
           }
           

           if (Request["IsCurrent"] != null) {
               //更新所有网站不为当前
               DBHelper.ExecuteNonQuery("Update E_Site set IsCurrent=0");
               //更新当前网站
               DBHelper.ExecuteNonQuery("Update E_Site set IsCurrent=1 where id=" + catcheSiteID);
            
           }
           stModl = CommonCacheManager.GetCacheData<SiteModel>("stModl" + AdminUser.Id); 
           if (stModl != null)
           {
               CurrentSite = stModl;
             
           }
           else
           {

               //默认系统设置的当前网站
               CurrentSite = CommonCacheManager.GetCacheData<SiteModel>("CurrentSite" + AdminUser.Id); 
               if (CurrentSite == null)
               { 
                   CurrentSite = CenterService.siteService.GetCurrentSite(); 
               }
           }
           CommonCacheManager.AddCacheData<SiteModel>("CurrentSite", CurrentSite, 180);

           base.OnInit(e);
       }
    }
}
